package lib.quasar.widget.edit;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import android.widget.EditText;
import lib.quasar.util.LogUtil;
import lib.quasar.widget.R;

/**
 * description: 有清除功能,只有当有焦点和文本不为空的时候才显示
 * created by kalu on 17-10-17 下午12:10
 */
@SuppressLint("AppCompatCustomView")
public class NumEditText extends EditText {

    // 背景颜色
    private int backgroundColor;
    // 背景圆角
    private float backgroundCorners;
    // 背景边框颜色
    private int backgroundStoreColor;
    // 背景边框宽度
    private float backgroundStoreSize;

    private int maxInput = -1;
    private final GradientDrawable bg = new GradientDrawable();

    public NumEditText(Context context) {
        super(context);
    }

    public NumEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = null;
        try {
            typedArray = context.obtainStyledAttributes(attrs, R.styleable.NumEditText, 0, 0);
            maxInput = typedArray.getInt(R.styleable.NumEditText_net_input_max, maxInput);
            backgroundColor = typedArray.getColor(R.styleable.NumEditText_net_background_color, Color.TRANSPARENT);
            backgroundStoreColor = typedArray.getColor(R.styleable.NumEditText_net_background_store_color, Color.TRANSPARENT);
            backgroundCorners = typedArray.getDimension(R.styleable.NumEditText_net_background_corners, 0);
            backgroundStoreSize = typedArray.getDimension(R.styleable.NumEditText_net_background_store_size, 0);
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
        } finally {
            if (null == typedArray) return;
            typedArray.recycle();
        }
    }

    public NumEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /*****************************************************************************/

    @Override
    protected void onDraw(Canvas canvas) {

        // 圆角矩形背景
        bg.setCornerRadius(backgroundCorners);
        bg.setColor(backgroundColor);
        bg.setSize(getWidth(), getHeight());
        bg.setStroke((int) backgroundStoreSize, backgroundStoreColor);
        setBackgroundDrawable(bg);

        super.onDraw(canvas);
    }

    public void setInputMax(int inputMax) {
        this.maxInput = inputMax;
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        return new NumEditText.InputWrapper(super.onCreateInputConnection(outAttrs), false);
    }

    private final class InputWrapper extends InputConnectionWrapper implements InputConnection {

        public InputWrapper(InputConnection target, boolean mutable) {
            super(target, mutable);
        }

        @Override
        public boolean commitText(CharSequence text, int newCursorPosition) {

            final String before = getText().toString().trim();

            // 只能有1个小数点
            if (before.contains(".") && ".".equals(text)) return false;

            if (maxInput != -1) {
                try {
                    final String trim = getText().toString().trim() + text;
                    if (!TextUtils.isEmpty(trim) && Float.parseFloat(trim) > maxInput) {
                        setText("");
                        return false;
                    }
                } catch (Exception e) {
                    LogUtil.e("", e.getMessage(), e);
                }
            }
            return super.commitText(text, newCursorPosition);
        }

        @Override
        public boolean sendKeyEvent(KeyEvent event) {
            return super.sendKeyEvent(event);
        }

        @Override
        public boolean setSelection(int start, int end) {
            return super.setSelection(start, end);
        }
    }
}